package com.miao.im.core;

import com.miao.im.server.session.SessionContext;
import com.miao.im.support.SocketSessionHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/19 22:18
 * @Description:
 */
@Component
public class AsyncExecutor {
    @Autowired
    @Qualifier("bizMsgExecutor")
    private ThreadPoolTaskExecutor executor;

    /**
     * @Description: 执行命令
     * @Author: miaoguoxin
     * @Date: 2021/3/19
     */
    public void execute(SessionContext sessionContext, Runnable runnable) {
        executor.execute(() -> {
            try {
                SocketSessionHolder.set(sessionContext);
                runnable.run();
            } catch (Exception e) {
                sessionContext.getCtx().fireExceptionCaught(e);
            } finally {
                SocketSessionHolder.remove();
            }
        });
    }
}
